/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package idea.countries;


import java.awt.Point;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import org.netbeans.api.visual.action.AcceptProvider;
import org.netbeans.api.visual.action.ActionFactory;
import org.netbeans.api.visual.action.ConnectorState;
import org.netbeans.api.visual.graph.GraphScene;
import org.netbeans.api.visual.widget.LayerWidget;
import org.netbeans.api.visual.widget.Widget;
import org.netbeans.api.visual.widget.general.IconNodeWidget;
import org.openide.util.Exceptions;


/**
 *
 * @author rejun001
 */
public class GraphSceneImpl extends GraphScene<Country , String>
{

    private LayerWidget mainLayer;


    public GraphSceneImpl()
    {
        mainLayer = new LayerWidget( this );
        addChild( mainLayer );

        getActions().addAction( ActionFactory.createAcceptAction( new AcceptProvider()
        {
            @Override
            public ConnectorState isAcceptable( Widget widget , Point point , Transferable t )
            {
                return ConnectorState.ACCEPT;
            }


            @Override
            public void accept( Widget widget , Point point , Transferable t )
            {
                Country c = getCountryFromTransferable( t );
                Widget w = GraphSceneImpl.this.addNode( c );  // this calls attachNodeWidget below
                w.setPreferredLocation( widget.convertLocalToScene( point ) );
            }


            private Country getCountryFromTransferable( Transferable t )
            {
                Country c = null;
                try
                {
                    c = (Country) t.getTransferData( Country.DATA_FLAVOR );
                }
                catch ( UnsupportedFlavorException ex )
                {
                    Exceptions.printStackTrace( ex );
                }
                catch ( IOException ex )
                {
                    Exceptions.printStackTrace( ex );
                }
                return c;

            }

        } ) );
        
        getActions().addAction(ActionFactory.createZoomAction());
    }


    @Override
    protected Widget attachNodeWidget( Country node )
    {
        IconNodeWidget widget = new IconNodeWidget( this );
        widget.setLabel( node.getName() );
        widget.getActions().addAction( ActionFactory.createMoveAction() );
        mainLayer.addChild( widget );
        return widget;
    }


    @Override
    protected Widget attachEdgeWidget( String e )
    {
        return null;
    }


    @Override
    protected void attachEdgeSourceAnchor( String e , Country n , Country n1 )
    {
    }


    @Override
    protected void attachEdgeTargetAnchor( String e , Country n , Country n1 )
    {
    }

}
